package it.clariter.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Classe contenente utility varie per la 
 * gestione delle date
 * 
 * @version 0.1.0.0
 */
public class DateUtils {
	private final static SimpleDateFormat defaultSimpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
	private final static SimpleDateFormat extendedSimpleDateFormat24H = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
	private final static SimpleDateFormat simpleDateFormatAnnoMeseGiorno = new SimpleDateFormat("yyyyMMdd");
	
	public static final Date dataDefault1948=parseDateEvolution("01/01/1948"); 
	
	public static Date data9999(){
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
		try {
			cal.setTime(dateFormat.parse("31/12/9999"));
		} catch (ParseException e) {
			e.getMessage();
		}
		return cal.getTime();
	}

	public static Date parseDateEvolution(String strDate){
		Calendar cal = Calendar.getInstance();
		
		try {
			cal.setTime(defaultSimpleDateFormat.parse(strDate));
		} catch (ParseException e) {
			e.getMessage();
			return null;
		}
		return cal.getTime();
	}

	public static boolean compareOnlyDateTo(Date date1, Date date2) {
		boolean compareOk = true;
		SimpleDateFormat formatData = defaultSimpleDateFormat;
		if (!formatData.format(date1).equals(formatData.format(date2)))
			compareOk = false;
		return compareOk;
	}

	//aggiunge o sottrae giorni alla data: 
	//interi positivi: aggiunge
	//interi negativi: sottrae
	public Date addOrSubstractDaysToDate(Date dateInit, int days){
		Calendar cal = Calendar.getInstance();
		cal.setTime(dateInit);
		cal.add( Calendar.DAY_OF_MONTH, days);
		return cal.getTime();
	}
	
	public static Date addHoursToDate(Date dateInit, int hours){
		Calendar cal = Calendar.getInstance();
		cal.setTime(dateInit);
		cal.add( Calendar.HOUR, hours);
		return cal.getTime();
	}

	public static boolean periodiSovrapposti(Date dataIni1, Date dataFine1, Date dataIni2, Date dataFine2) {
		boolean periodiSovrapposti=false;
		
		// data1   		|----------						|----------
		// data2	|-----------		oppure	|-----------|		
		if (dataFine1==null){
			//if (dataIni2.compareTo(dataIni1)>0){
			if (dataFine2==null || dataFine2.compareTo(dataIni1)>=0){
				periodiSovrapposti=true;
			}	

		// data1   		|----------|	
		// data2	|-----------		
		}else{ 
			if (dataFine2==null){ 
				if (dataIni2.compareTo(dataFine1)<=0){
					periodiSovrapposti=true;
				}
		// data1   |----------|							|----------|
		// data2		|----------|	oppure		|----------|
			}else if (! (dataFine1.compareTo(dataIni2)<0 || dataIni1.compareTo(dataFine2)>0) ){
				periodiSovrapposti=true;
			}
		}
		
		return periodiSovrapposti;
	}

	public static Date clearHours(Date date) {
		if(date == null)
			return null;
//		System.out.println(date);
		Calendar c=Calendar.getInstance();
		c.setTime(date);
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		c.set(Calendar.MILLISECOND, 0);
//		System.out.println(c.getTime());
		return c.getTime();
	}
	public static Date getCurrentDate() {
		Calendar c=Calendar.getInstance();
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		c.set(Calendar.MILLISECOND, 0);
//		System.out.println(c.getTime());
		return c.getTime();
	}
	
	//ritorna un tipo data da una stringa secondo il formato prestabilito
	public static Date parseDate(String strDate) throws ParseException{
		return strDate != null ? defaultSimpleDateFormat.parse(strDate) : null;
	}
	
	//ritorna un tipo timestamp da una stringa secondo il formato prestabilito
	public static Date parseTimestamp(String strTimestamp) throws ParseException{
		return strTimestamp != null ? extendedSimpleDateFormat24H.parse(strTimestamp) : null;
	}
	
	//ritorna un tipo stringa da una data secondo il formato prestabilito
	public static String parseString(Date dateDate){
		return dateDate != null ? defaultSimpleDateFormat.format(dateDate) : "";
	}
	
	public static String parseStringTS(Date dateDate){
		return dateDate != null ? extendedSimpleDateFormat24H.format(dateDate) : "";
	}
	
	public static String getDateFormatAnnoMeseGiorno(Date dateDate){
		return dateDate != null ? DateUtils.simpleDateFormatAnnoMeseGiorno.format(dateDate):null;
	}
	
}
